<template>
  <div class="page">
    <el-table v-loading="loading" :data="playList" stripe style="width: 100%">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="封面" width="100">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" height="50" />
        </template>
      </el-table-column>
      <el-table-column prop="copywriter" label="描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="onEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="onDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="提示" :visible.sync="delDialogVisible" width="30%">
      <span>确定删除该歌单吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doDel">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, del } from "@/api/playlist";
import scroll from "@/utils/scroll";

export default {
  components: {},
  props: {},
  data() {
    return {
      playList: [],
      count: 50,
      loading: false,
      delDialogVisible: false, // 删除歌单的对话框是否显示
      info: {}
    };
  },
  watch: {},
  computed: {},
  methods: {
    getList() {
      this.loading = true;
      fetchList({
        start: this.playList.length,
        count: this.count
      }).then(res => {
        this.loading = false;
        // console.log(res);
        if (res.data.length < this.count) {
          scroll.end();
        }
        this.playList = this.playList.concat(res.data);
      });
    },
    onEdit(row) {
      this.$router.push(`/playlist/edit/${row._id}`);
    },
    onDel(row) {
      this.delDialogVisible = true;
      this.info.id = row._id;
    },
    doDel() {
      const id = this.info.id;
      del({ id: this.info.id }).then(res => {
        this.delDialogVisible = false;
        if (parseInt(res.data.deleted) > 0) {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.playList = [];
          this.getList();
        } else {
          this.$message.error("删除失败");
        }
      });
    }
  },
  created() {
    this.getList();
  },
  mounted() {
    scroll.start(this.getList);
  }
};
</script>
<style lang="scss" scoped>
</style>